/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newOperatorGuide.java
 *
 * Created on 29/05/2011, 15:48:44
 */
package view;

import components.Op_Generico;
import components.Op_GenericoData;
import controler.Action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author felipelageduarte
 */
public class newOperatorGuide extends javax.swing.JFrame {

  /** Creates new form newOperatorGuide */
  public newOperatorGuide() {
    initComponents();
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jTabbedPane1 = new javax.swing.JTabbedPane();
    arquivoPanel = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    path = new javax.swing.JTextField();
    abrirButton1 = new javax.swing.JButton();
    jLabel2 = new javax.swing.JLabel();
    name = new javax.swing.JTextField();
    entradaPanel = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    entradaTable = new javax.swing.JTable();
    entradaSpinner = new javax.swing.JSpinner();
    jLabel3 = new javax.swing.JLabel();
    saidaPanel = new javax.swing.JPanel();
    jScrollPane2 = new javax.swing.JScrollPane();
    saidaTable = new javax.swing.JTable();
    saidaSpinner = new javax.swing.JSpinner();
    jLabel4 = new javax.swing.JLabel();
    controlePanel = new javax.swing.JPanel();
    jScrollPane3 = new javax.swing.JScrollPane();
    controleTable = new javax.swing.JTable();
    controleSpinner = new javax.swing.JSpinner();
    jLabel5 = new javax.swing.JLabel();
    jScrollPane4 = new javax.swing.JScrollPane();
    Source = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setResizable(false);

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Novo Operador"));

    jButton1.setText("Criar");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    jButton2.setText("Cancelar");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton2ActionPerformed(evt);
      }
    });

    jLabel1.setText("Arquivo Fonte:");

    path.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        pathActionPerformed(evt);
      }
    });

    abrirButton1.setText("Abrir");
    abrirButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        abrirButton1ActionPerformed(evt);
      }
    });

    jLabel2.setText("Nome do operador: ");

    name.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        nameActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout arquivoPanelLayout = new javax.swing.GroupLayout(arquivoPanel);
    arquivoPanel.setLayout(arquivoPanelLayout);
    arquivoPanelLayout.setHorizontalGroup(
      arquivoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(arquivoPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(arquivoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(arquivoPanelLayout.createSequentialGroup()
            .addComponent(jLabel2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(name, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, arquivoPanelLayout.createSequentialGroup()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(path, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(abrirButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );
    arquivoPanelLayout.setVerticalGroup(
      arquivoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(arquivoPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(arquivoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(18, 18, 18)
        .addGroup(arquivoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel1)
          .addComponent(abrirButton1))
        .addContainerGap(140, Short.MAX_VALUE))
    );

    jTabbedPane1.addTab("Arquivo", arquivoPanel);

    entradaTable.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "Nome", "Tipo"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.String.class, java.lang.String.class
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }
    });
    jScrollPane1.setViewportView(entradaTable);

    entradaSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
    entradaSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
      public void stateChanged(javax.swing.event.ChangeEvent evt) {
        entradaSpinnerStateChanged(evt);
      }
    });

    jLabel3.setText("Número de entradas:");

    javax.swing.GroupLayout entradaPanelLayout = new javax.swing.GroupLayout(entradaPanel);
    entradaPanel.setLayout(entradaPanelLayout);
    entradaPanelLayout.setHorizontalGroup(
      entradaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(entradaPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel3)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(entradaSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(269, Short.MAX_VALUE))
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
    );
    entradaPanelLayout.setVerticalGroup(
      entradaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, entradaPanelLayout.createSequentialGroup()
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(entradaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(entradaSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel3))
        .addContainerGap())
    );

    jTabbedPane1.addTab("Entrada", entradaPanel);

    saidaTable.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "Nome", "Tipo"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.String.class, java.lang.String.class
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }
    });
    jScrollPane2.setViewportView(saidaTable);

    saidaSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
    saidaSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
      public void stateChanged(javax.swing.event.ChangeEvent evt) {
        saidaSpinnerStateChanged(evt);
      }
    });

    jLabel4.setText("Número de entradas:");

    javax.swing.GroupLayout saidaPanelLayout = new javax.swing.GroupLayout(saidaPanel);
    saidaPanel.setLayout(saidaPanelLayout);
    saidaPanelLayout.setHorizontalGroup(
      saidaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(saidaPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel4)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(saidaSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(269, Short.MAX_VALUE))
      .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
    );
    saidaPanelLayout.setVerticalGroup(
      saidaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, saidaPanelLayout.createSequentialGroup()
        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(saidaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(saidaSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel4))
        .addContainerGap())
    );

    jTabbedPane1.addTab("Saída", saidaPanel);

    controleTable.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "Nome", "Tipo"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.String.class, java.lang.String.class
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }
    });
    jScrollPane3.setViewportView(controleTable);

    controleSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
    controleSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
      public void stateChanged(javax.swing.event.ChangeEvent evt) {
        controleSpinnerStateChanged(evt);
      }
    });

    jLabel5.setText("Número de entradas:");

    javax.swing.GroupLayout controlePanelLayout = new javax.swing.GroupLayout(controlePanel);
    controlePanel.setLayout(controlePanelLayout);
    controlePanelLayout.setHorizontalGroup(
      controlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(controlePanelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel5)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(controleSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(269, Short.MAX_VALUE))
      .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
    );
    controlePanelLayout.setVerticalGroup(
      controlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controlePanelLayout.createSequentialGroup()
        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(controlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(controleSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel5))
        .addContainerGap())
    );

    jTabbedPane1.addTab("Controle", controlePanel);

    Source.setColumns(20);
    Source.setRows(5);
    jScrollPane4.setViewportView(Source);

    jTabbedPane1.addTab("Código Fonte", jScrollPane4);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
        .addContainerGap(315, Short.MAX_VALUE)
        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
      .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jButton1)
          .addComponent(jButton2))
        .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void pathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathActionPerformed
}//GEN-LAST:event_pathActionPerformed

  private void abrirButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirButton1ActionPerformed
    path.setText(pathWindows(path.getText(), "Selecione o Diretorio de Indices:"));
    preProcessarArquivo(path.getText());
}//GEN-LAST:event_abrirButton1ActionPerformed

  private void nameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameActionPerformed
    // TODO add your handling code here:
  }//GEN-LAST:event_nameActionPerformed

  private void preProcessarArquivo(String src) {
    InputStream in;
    byte[] buf = new byte[1024];
    int len;
    try {
      in = new FileInputStream(src);

      // Copia dos arquivos
      while (in.read(buf) > 0) {
        Source.setText(Source.getText() + new String(buf).trim().toString());
        buf = new byte[1024];
      }

      //fecha o arquivo lido e o copiado
      in.close();
    } catch (Exception ex) {
      return;
    }
    
    String []s = Source.getText().split(" ");
    for(int i = 0; i < s.length; i++){
      if(s[i].compareToIgnoreCase("port") == 0){
        
      }
    }
    
    
  }

  private void entradaSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_entradaSpinnerStateChanged
    int numRows = (Integer) entradaSpinner.getValue();
    int tableRows = entradaTable.getModel().getRowCount();
    if (numRows > tableRows) {
      DefaultTableModel model = (DefaultTableModel) entradaTable.getModel();
      String[] arendt = {"", ""};
      model.addRow(arendt);
    } else if (numRows < tableRows) {
      DefaultTableModel model = (DefaultTableModel) entradaTable.getModel();
      model.removeRow(model.getRowCount() - 1);
    }
  }//GEN-LAST:event_entradaSpinnerStateChanged

  private void saidaSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_saidaSpinnerStateChanged
    int numRows = (Integer) saidaSpinner.getValue();
    int tableRows = saidaTable.getModel().getRowCount();
    if (numRows > tableRows) {
      DefaultTableModel model = (DefaultTableModel) saidaTable.getModel();
      String[] arendt = {"", ""};
      model.addRow(arendt);
    } else if (numRows < tableRows) {
      DefaultTableModel model = (DefaultTableModel) saidaTable.getModel();
      model.removeRow(model.getRowCount() - 1);
    }
  }//GEN-LAST:event_saidaSpinnerStateChanged

  private void controleSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_controleSpinnerStateChanged
    int numRows = (Integer) controleSpinner.getValue();
    int tableRows = controleTable.getModel().getRowCount();
    if (numRows > tableRows) {
      DefaultTableModel model = (DefaultTableModel) controleTable.getModel();
      String[] arendt = {"", ""};
      model.addRow(arendt);
    } else if (numRows < tableRows) {
      DefaultTableModel model = (DefaultTableModel) controleTable.getModel();
      model.removeRow(model.getRowCount() - 1);
    }
  }//GEN-LAST:event_controleSpinnerStateChanged

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    this.dispose();
  }//GEN-LAST:event_jButton2ActionPerformed

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    Action.getInstance().newOp_Generico(
            new Op_GenericoData(name.getText(), 
            entradaTable.getModel().getRowCount(),
            saidaTable.getModel().getRowCount(),
            path.getText())
    );

    this.dispose();
  }//GEN-LAST:event_jButton1ActionPerformed

  private String pathWindows(String currrentDirectory, String title) {
    String path = null;
    JFileChooser fc = new JFileChooser();
    if (currrentDirectory == null) {
      fc.setCurrentDirectory(new java.io.File("."));
    } else {
      fc.setCurrentDirectory(new java.io.File(currrentDirectory));
    }
    fc.setDialogTitle(title);
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fc.setAcceptAllFileFilterUsed(false);
    fc.setFileFilter(new FileNameExtensionFilter("Codigo Fonte VHDL (*.vhd)", "vhd"));
    if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
      return fc.getSelectedFile().getAbsolutePath();
    }
    return path;
  }

  // Copies src file to dst file.
  // If the dst file does not exist, it is created
  private void copy(File src, File dst) throws Exception {
    InputStream in;
    OutputStream out;
    byte[] buf = new byte[1024];
    int len;
    try {
      in = new FileInputStream(src);
      out = new FileOutputStream(dst); // Transfer bytes from in to out

      // Copia dos arquivos
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }

      //fecha o arquivo lido e o copiado
      in.close();
      out.close();

    } catch (Exception ex) {
      throw new Exception(ex.toString());
    }
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
        new newOperatorGuide().setVisible(true);
      }
    });
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JTextArea Source;
  private javax.swing.JButton abrirButton1;
  private javax.swing.JPanel arquivoPanel;
  private javax.swing.JPanel controlePanel;
  private javax.swing.JSpinner controleSpinner;
  private javax.swing.JTable controleTable;
  private javax.swing.JPanel entradaPanel;
  private javax.swing.JSpinner entradaSpinner;
  private javax.swing.JTable entradaTable;
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane2;
  private javax.swing.JScrollPane jScrollPane3;
  private javax.swing.JScrollPane jScrollPane4;
  private javax.swing.JTabbedPane jTabbedPane1;
  private javax.swing.JTextField name;
  private javax.swing.JTextField path;
  private javax.swing.JPanel saidaPanel;
  private javax.swing.JSpinner saidaSpinner;
  private javax.swing.JTable saidaTable;
  // End of variables declaration//GEN-END:variables
}
